<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\lose\controller;

use cmf\controller\HomeBaseController;
use think\Db;
use app\admin\model\NavMenuModel;

class MoveDataController extends HomeBaseController
{
    public function index()
    {
        return '移动数据';
    }

    public function test(){
        $a = db('lose_category')
            ->where('status',1)
            ->where('id',['<',10],['>',100],'or')
            ->select(false);
            // ->buildSql();

        echo $a;die;
    }

    public function more(){
        $table = 'lose_post';
        $lists = db($table)->where('more','like','%jinfakeji.gz12.hostadm.net%')->select();
        // echo db($table)->getLastSql();die;
        // pre($lists,1);

        foreach ($lists as $k => $v) {

            $v['more'] = json_decode($v['more'],true);
            foreach ($v['more']['photos'] as $key => &$val) {
                $val['url'] = str_replace('http://jinfakeji.gz12.hostadm.net/data/upload/','',$val['url']);
            }


            $data = [
                'id'   => $v['id'],
                'more' => json_encode($v['more']),
            ];
            // pre($data,1);
            $res = Db::name($table)->update($data);
            
        }
        // pre($res);

        pre('success',1);        
    }

    public function move_goods_data(){
        $lists = Db::table('hunuo_goods')->select();
        foreach ($lists as $k => $v) {
            $data = [
                'goods_id'  => $v['goods_id'],
                'post_hits' => $v['post_hits'],
            ];
            
            $res = Db::name('goods')->update($data);
        }
        // pre($res);

        pre('success',1);        
    }

    //复制文章数据
    public function move_branch_data(){
        $lists = Db::table('hunuo_branch_posts')->select();
        foreach ($lists as $k => $v) {
            $data = [
                'id'        => $v['id'],
                'post_hits' => $v['post_hits'],
            ];
            
            $res = Db::name('branch_post')->update($data);
        }
        // pre($res);

        pre('success',1);        
    }

    public function goods_thumb(){
        $lists = db('branch_post')->where('more','like','{"thumb"%')->select();
        // pre($lists,1);
        foreach ($lists as &$v) {
            $v['more'] = json_decode($v['more'],true);
            $v['more']['thumbnail'] = $v['more']['thumb'];
            unset($v['more']['thumb']);
            $v['more'] = json_encode($v['more']);
            db('branch_post')->update($v);
        }

        
    }

    public function goods_date(){
        $lists = db('goods')->order('goods_id desc')->select();
        foreach ($lists as &$v) {
            $time = strtotime($v['post_date']);
            $v['create_time']=$v['update_time'] =$v['published_time']= $time;
            db('goods')->update($v);
        }
    }

    //产品分类缩略图
    public function goods_cat(){
        $lists = db('goods_category')->where('id','<',107)->order('id desc')->select();
        
        foreach ($lists as &$v) {
            $more                  = [];
            $more['thumbnail']     = $v['thumb'];
            $more['cat_img']       = $v['cat_img'];
            $more['index_cat_img'] = $v['index_cat_img'];
            $v['more'] = json_encode($more);
            db('goods_category')->update($v);
        }
    }

    public function nav_menu(){
        $lists = db('nav_menu')->select();
        foreach ($lists as &$v) {
            $v['href'] = json_decode($v['href'],true);
            $v['href']['param']['id'] = intval($v['href']['param']['id']);
            $v['href'] = json_encode($v['href']);
            db('nav_menu')->update($v);
        }
    }

    //后台菜单
    public function move_menu(){
        $where = [
            'model' => ['LIKE','%goods%'],
            // 'id'    => ['<>','222'],
        ];
        $lists = Db::table('hunuo_menu') ->where($where) ->select();
        // pre($lists,1);
        foreach ($lists as $k => $v) {
            $data = [
                'id'         => $v['id'],
                'parent_id'  => $v['parentid'],
                'type'       => 2,
                
                'app'        => strtolower($v['app']),
                'controller' => $v['model'],
                'action'     => $v['action'],
                'name'       => $v['name'],
            ];
            
            $all_data[] = $data;
        }
        // pre($all_data,1);
        // pre($lists,1);
        $res = Db::name('admin_menu')->insertAll($all_data);
        pre($res);

        pre('success',1);
    }    

    //友情链接
    public function move_links(){
        $lists = Db::table('hunuo_links')->select();
        // pre($lists,1);
        foreach ($lists as $k => $v) {
            $data = [
                'id'         => $v['link_id'],
                'status'     => 1,
                'rating'     => 1,
                'list_order' => $v['listorder'],
                'url'        => $v['link_url'],
                'name'       => $v['link_name'],
                'target'     => $v['link_target'],
            ];
            
            $all_data[] = $data;
        }
        // pre($all_data,1);
        // pre($lists,1);
        $res = Db::name('link')->insertAll($all_data);
        pre($res);

        pre('success',1);
    }

    // 替换文章文件存放路径
    public function post_replac_file_url(){
        $lists = Db::name('lose_post')->field('id,file_url')->where('file_url','like','%file_material%')->select();
        // foreach ($lists as $k => &$v) {
        //     $v['file_url'] = str_replace('file_material','fileMaterial',$v['file_url']);

        //     // pre($v,1);
        //     Db::name('lose_post')->update($v);
        // }
        pre($lists,1);
    }

    //
    public function move_slide(){
        $lists = Db::table('hunuo_slide')->select();
        // pre($lists,1);
        foreach ($lists as $k => $v) {
            $v['listorder'] = empty($v['listorder'])? 10000 :$v['listorder'];
            $data = [
                'id'          => $v['slide_id'],
                'slide_id'    => $v['slide_cid'],
                'title'       => $v['slide_name'],
                'image'       => $v['slide_pic'],
                'url'         => $v['slide_url'],
                'description' => $v['slide_des'],
                'status'      => $v['slide_status'],
                'list_order'  => $v['listorder'],
                'content'     => $v['slide_content'],
            ];
            
            $all_data[] = $data;
        }
        // pre($all_data,1);
        // pre($lists,1);
        $res = Db::name('slide_item')->insertAll($all_data);
        pre($res);

        pre('success',1);
    }

    public function move_slide_cat(){
        $lists = Db::table('hunuo_slide_cat')->select();
        foreach ($lists as $k => $v) {

            $data = [
                'id'     => $v['cid'],
                'name'   => $v['cat_name'],
                'remark' => $v['cat_remark'],
                'status' => $v['cat_status'],
            ];
            
            $all_data[] = $data;
        }
        // pre($all_data,1);
        // pre($lists,1);
        $res = Db::name('slide')->insertAll($all_data);
        pre($res);

        pre('success',1);
    }

    public function move_nav(){
        $navMenuModel = new NavMenuModel();
        $main_nav = $navMenuModel->hNavMenusTreeArray(1);
        foreach ($main_nav as $k => $v) {

            $data = [
                'id'         => $v['id'],
                'nav_id'     => $v['cid'],
                'parent_id'  => $v['parentid'],
                'status'     => $v['status'],
                'list_order' => $v['listorder'],
                'name'       => $v['label'],
                'path'       => $v['path'],
                'thumb'       => $v['thumb'],
                'target'     => '',
                'href'       => '',
                'icon'       => '',
            ];

            if(empty($data['list_order']) ){
                $data['list_order'] = 10000;
            }

            if(!empty($v['url']) ){
                $data['href'] = $v['url'];
            }
            
            $all_data[] = $data;
        }
        // pre($all_data,1);
        // pre($lists,1);
        $res = Db::name('nav_menu')->insertAll($all_data);
        pre($res);
        pre('success',1);

    }

    //复制分类数据
    public function move_term_data(){
        $lists = Db::table('hunuo_terms')->select();
        foreach ($lists as $k => $v) {
            $data = [
                'id'              => $v['term_id'],
                'title'           => $v['title'],
                'type'            => $v['type'],
                'taxonomy'        => $v['taxonomy'],
                'parent_id'       => $v['parent'],
                'post_count'      => $v['count'],
                'list_order'      => $v['listorder'],
                'name'            => $v['name'],
                'description'     => $v['description'],
                'path'            => $v['path'],
                'seo_title'       => $v['seo_title'],
                'seo_keywords'    => $v['seo_keywords'],
                'seo_description' => $v['seo_description'],
                'list_tpl'        => $v['list_tpl'],
                'one_tpl'         => $v['one_tpl'],
                'status'          => $v['status'],
                'thumb'           => $v['thumb'],
                'slide_id'       => $v['slide_cid'],
                'thumb_size'      => $v['thumb_size'],
                'content'         => $v['content'],
                'content2'        => $v['content2'],
                'more'            => '{"thumbnail":""}',
            ];

            $all_data[] = $data;
        }
        // pre($all_data,1);
        // pre($lists,1);
        $res = Db::name('lose_category')->insertAll($all_data);

        pre($res);

        pre('success',1);
    }

     public function move_term_rel_data(){
        $lists = Db::table('hunuo_term_relationships')->select();
        foreach ($lists as $k => $v) {
            $data = [
                'id'          => $v['tid'],
                'post_id'     => $v['object_id'],
                'category_id' => $v['term_id'],
                'list_order'  => $v['listorder'],
                'status'      => $v['status'],
            ];

            $all_data[] = $data;
        }
        // pre($all_data,1);
        // pre($lists,1);
        $res = Db::name('lose_category_post')->insertAll($all_data);

        pre($res);

        pre('success',1);        
    }

     public function move_b_term_rel_data(){
        $lists = Db::table('hunuo_branch_term_relationships')->select();
        foreach ($lists as $k => $v) {
            $data = [
                'id'          => $v['tid'],
                'post_id'     => $v['object_id'],
                'category_id' => $v['term_id'],
                'branch_id'   => $v['branch_id'],
                'list_order'  => $v['listorder'],
                'status'      => $v['status'],
            ];

            $all_data[] = $data;
        }
        // pre($all_data,1);
        // pre($lists,1);
        $res = Db::name('branch_category_post')->insertAll($all_data);

        pre($res);

        pre('success',1);        
    }

    //复制文章数据
    public function move_post_data(){
        $lists = Db::table('hunuo_posts')->where('post_status=1 AND post_type=1')->select();
        foreach ($lists as $k => $v) {
            $smeta = '';
            $smeta = json_decode($v['smeta'],true);
            $smeta['thumbnail'] = $smeta['thumb'];
            unset($smeta['thumb']);
            if( !empty($smeta['photo']) ){
                $smeta['photos'] = $smeta['photo'];
                unset($smeta['photo']);
            }

            if( $v['id']!=81 ){
                unset($smeta['founding_time']);
                unset($smeta['founding_thumb']);
                unset($smeta['sales_volume']);
                unset($smeta['business_income']);
                unset($smeta['total_assets']);
                unset($smeta['info']);
                unset($smeta['video_thumb']);
                unset($smeta['music_title']);
                unset($smeta['music_author']);
                unset($smeta['music_thumb']);
                unset($smeta['music']);
                unset($smeta['music_name']);
                unset($smeta['music_lrc']);
            }

            $data = [
                'id'                    => $v['id'],
                'user_id'               => $v['post_author'],
                'post_status'           => 99,
                'is_top'                => $v['istop'],
                'recommended'           => $v['recommended'],
                'post_hits'             => $v['post_hits'],
                'post_like'             => $v['post_like'],
                'create_time'           => $v['add_time'],
                'update_time'           => strtotime($v['post_modified']),
                'published_time'        => strtotime($v['post_date']),
                'post_title'            => $v['post_title'],
                'post_keywords'         => $v['post_keywords'],
                'post_excerpt'          => $v['post_excerpt'],
                'post_source'           => $v['post_source'],
                'post_content'          => $v['post_content'],
                'more'                  => json_encode($smeta),
                'file_url'              => $v['file_url'],
                'file_name'             => $v['oldname'],
                'href'                  => $v['href'],
            ];
         
            $all_data[] = $data;
        }
        // pre($all_data,1);
        // pre($lists,1);
        $res = Db::name('lose_post')->insertAll($all_data);

        pre($res);

        pre('success',1);        
    }

    public function del_post(){
        $ids     = Db::name('lose_post')->where('post_status',3)->column('id');
        $recycle = Db::name('lose_post')->where(['id' => ['in', $ids]])->select();
        $result  = Db::name('lose_post')
                -> where(['id' => ['in', $ids]])
                -> update(['delete_time' => time(),'post_status'=>1]);
                
        if ($result) {
            foreach ($recycle as $value) {
                $data = [
                    'object_id'   => $value['id'],
                    'create_time' => time(),
                    'table_name'  => 'lose_post',
                    'name'        => $value['post_title']
                ];
                Db::name('recycleBin')->insert($data);
            }
            $this->success("删除成功！", '');
        }
    }


    // 删除文章，加入回收站
    public function del_data(){
        $ids = Db::name('lose_post')->where('post_status','3')->order('id asc')->column('id');
        
        $recycle = Db::name('lose_post')->where(['id' => ['in', $ids]])->select();
        $result  = Db::name('lose_post')->where(['id' => ['in', $ids]])->update(['delete_time' => time()]);
        if ($result) {
            foreach ($recycle as $value) {
                $data = [
                    'object_id'   => $value['id'],
                    'create_time' => time(),
                    'table_name'  => 'lose_post',
                    'name'        => $value['post_title']
                ];
                Db::name('recycleBin')->insert($data);
            }
            $this->success("删除成功！", '');
        }
    }

    //编辑器内容，图片地址替换
    public function content_data(){
        $table = 'lose_post';
        $lists = Db::name($table)->field('id,post_content')->where('post_content','like','%jinfakeji.gz12.hostadm.net%')->order('id asc')->select();
        // echo db($table)->getLastSql();die;
        // pre( $lists,1 );
        
        foreach ($lists as  &$v) {
            $v['post_content'] = str_replace('http://jinfakeji.gz12.hostadm.net/data/upload/','',$v['post_content']);;
            Db::name($table)->update($v);
        }

        // $lists = Db::name('goods')->field('goods_id,post_content')->where('post_content','like','%src="/data/upload/ueditor/2017%')->select();
        // foreach ($lists as  &$v) {
            
        //     $v['post_content'] = str_replace('src="/data/upload/ueditor/','src="/upload/ueditor/',$v['post_content']);
        //     Db::name('goods')->update($v);
        //     // pre( $v,1 );
        // }
    }
}
